package com.hy.study.bean;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;
import org.springframework.web.client.RestTemplate;

import javax.servlet.MultipartConfigElement;

/**
 * @author huyong
 * @date 2024 年 01 月 05 日 17:10: 35
 */
@Configuration
public class ConfigBean {
    /**
     * 将springboot 默认的Jackson 改成阿里的 FastJson
     *
     * @return
     */
    @Bean
    public HttpMessageConverters httpMessageConverters() {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        converter.setFeatures(SerializerFeature.PrettyFormat);
        return new HttpMessageConverters(converter);
    }

    /**
     * 对上传的文件进行限制
     */

    @Bean
    public MultipartConfigElement multipartConfigFactory() {
        MultipartConfigFactory multipartConfigFactory = new MultipartConfigFactory();
        multipartConfigFactory.setMaxFileSize(DataSize.ofMegabytes(128));// 设置单个文件的大小
        multipartConfigFactory.setMaxRequestSize(DataSize.ofMegabytes(1024));
        multipartConfigFactory.setLocation("/");// 上传文件的路径
        return multipartConfigFactory.createMultipartConfig();
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
